package icbc.nav.control
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import icbc.app.control.Alert;
	import icbc.nav.entity.Content;
	import icbc.nav.entity.Tip;
	
	public class TipsControl extends Sprite
	{
		protected var panel:Sprite;
		public var viewWidth:uint;
		public var viewHeight:uint;
		public var content:Content;
		private var tips:Vector.<Tip>;
		
		public function TipsControl(content:Content, viewWidth:uint, viewHeight:uint)
		{
			this.content = content;
			this.tips = content.tips;
			this.viewWidth = viewWidth;
			this.viewHeight = viewHeight;
			super();
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(Event.RESIZE, initMain);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function initMain(evt:Event = null):void
		{
			if(stage.stageWidth>0)
				stage.removeEventListener(Event.RESIZE, initMain);
			else return;
			initContent();
		}
		
		private function initContent():void
		{
			panel = buildPlainPanel();
			addChild(panel);
			
			var gapx:uint = 5;
			var posx:uint = gapx;
			var panelHeightHalf:uint = panel.height/2;
			
			for each(var item:Tip in tips)
			{
				var control:TipControl = new TipControl({label:item.name, source:item});
				control.x = item.x;
				control.y = item.y;
				control.addEventListener(TipControl.EVENT_CLICK, tipClickHandler);
				panel.addChild(control);
			}
		}
		
		private function tipClickHandler(evt:Event):void
		{
			if(checkTipExist()) return;
			var control:TipControl = evt.target as TipControl;
			var tip:Tip = control.data.source;

			var tooltip:TooltipImageControl = new TooltipImageControl({image:tip.image, tip:control});
			tooltip.name = "tooltip";
			panel.addChild(tooltip);
			tooltip.x = control.x + 15;
			tooltip.y = control.y + control.height - 5;
			tooltip.onReady.add(toolImageReadyHandler);
		}
		
		private function toolImageReadyHandler():void
		{
			var control:TooltipImageControl;
			if(panel.getChildByName("tooltip") != null)
			{
				control = panel.getChildByName("tooltip") as TooltipImageControl;
			}
			if(control == null) return;
			var tipParent:TipControl = control.data.tip as TipControl;
			if( (control.y+control.height+50) > stage.stageHeight)
			{
				control.y = stage.stageHeight - control.height - 50 - 10;
				if(control.height > stage.stageHeight)
					control.y = stage.stageHeight - control.height;
			}
			var msg:String = "";
			msg = "-->"+ control.width + "\r";
			msg += "-->stage.width:"+ stage.stageWidth + "\r";
			msg += "--><:"+ (control.x+control.width) + "\r";
			//Alert.show(msg);
			if( (control.x+control.width+2) > stage.stageWidth)
			{
				control.x = stage.stageWidth - control.width - 30;
				if(control.width > stage.stageWidth)
					control.x = stage.stageWidth - control.width;
			}
		}
		
		private function checkTipExist():Boolean
		{
			if(panel.getChildByName("tooltip")!=null)
			{
				var control:TooltipImageControl = panel.getChildByName("tooltip") as TooltipImageControl;
				control.dispose();
				return true;
			}
			return false;
		}
		
		private function buildPlainPanel():Sprite
		{
			var base:Sprite = new Sprite();
			base.graphics.beginFill(0xE6E8D8,0);
			base.graphics.drawRect(0, 0, viewWidth, viewHeight);
			base.graphics.endFill();
			return base;
		}
	}
}